/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package redis.manage;

/**
 *
 * @author dell
 */
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPool;
import conf.XmlUtil;
public class RedisPool
{
//columns_______________________________________________________________________    
protected int maxidle;
protected int minidle;
protected int maxtotal;
protected int maxwait;
protected String host;
protected int port;
protected String password;
protected int defaultdb;
protected JedisPoolConfig conf;
protected JedisPool pool;
protected boolean initialized;
//functions_____________________________________________________________________
     //constructor______________________________________________________________
public RedisPool()
{
     
}
     //getter___________________________________________________________________
public int getMaxidle()
{
     return maxidle;
}
public int getMinidle() 
{
     return minidle;
}
public int getMaxtotal() 
{
     return maxtotal;
}
public int getMaxwait() 
{
     return maxwait;
}
public String getHost() 
{
     return host;
}
public int getPort()
{
     return port;
}
public String getPassword() 
{
     return password;
}
public int getDefaultdb() 
{
     return defaultdb;
}
public boolean isInitialized() 
{
     return initialized;
}
     //setter___________________________________________________________________
public void setMaxidle(int maxidle) 
{
     this.maxidle = maxidle;
}
public void setMinidle(int minidle) 
{
     this.minidle = minidle;
}
public void setMaxtotal(int maxtotal) 
{
     this.maxtotal = maxtotal;
}
public void setMaxwait(int maxwait) 
{
     this.maxwait = maxwait;
}
public void setHost(String host) 
{
     this.host = host;
}
public void setPort(String port)
{
     this.password = port;
}
public void setPassword(String password) 
{
     this.password = password;
}
public void setDefaultdb(int defaultdb) 
{
     this.defaultdb = defaultdb;
}
public void setInitialized(boolean initialized) 
{
     this.initialized = initialized;
}
     //opeartor_________________________________________________________________
         //init functions_______________________________________________________
/**
 * @param property
 * @param value
 * @throws Exception 
 * @pass
 */
protected void set(String property,String value) throws Exception
{
     switch(property)
     {
         case "maxIdle":this.maxidle=Integer.valueOf(value);break;
         case "minIdle":this.maxidle=Integer.valueOf(value);break;
         case "maxTotal":this.maxtotal=Integer.valueOf(value);break;
         case "maxWait":this.maxwait=Integer.valueOf(value);break;
         case "host":this.host=value;break;
         case "port":this.port=Integer.valueOf(value);break;
         case "password":this.password=value;break;
         case "defaultDb":this.defaultdb=Integer.valueOf(value);break;
         default:throw new Exception("Unknown tag property:"+property);
     }
}
/**
 * @param url
 * @return
 * @throws Exception 
 * @pass
 */
protected void initProperty(String url) throws Exception
{
     NodeList list=XmlUtil.getNodeList(url, "configuration");
     for(int i=0;i<list.getLength();i++)
     {
         Node node=list.item(i);
         NodeList childNodes=node.getChildNodes();
         for(int j=0;j<childNodes.getLength();j++)
         {
             Node n=childNodes.item(j);
             if(n.getNodeType()==Node.ELEMENT_NODE)
             {
                 String property=n.getNodeName();
                 String value=n.getFirstChild().getNodeValue();
                 this.set(property, value);
             }
         }
     }
}
/**
 * @throws Exception 
 * @pass
 */
protected void initPool() throws Exception
{
     conf=new JedisPoolConfig();
     conf.setMaxIdle(maxidle);
     conf.setMinIdle(minidle);
     conf.setMaxTotal(maxtotal);
     conf.setMaxWaitMillis(maxwait);
     conf.setTestOnBorrow(true);
//     pool=new JedisPool(conf,host,port);
     pool=new JedisPool(host,port);
}
/**
 * init the jedis pool,url is the path of configuration xml file
 * @param url 
 * @pass
 */
protected void init(String url)
{
     try
     {
         this.initProperty(url);
         this.initPool();
         this.initialized=true;
     }
     catch(Exception e)
     {
         System.err.println(e);
     }
}
         //getJedis_____________________________________________________________
/**
 * get a Jedis instance from the Jedis Pool
 * @return Jedis Instance
 */
protected Jedis getJedis()
{
     Jedis jedis=null;
     if(this.initialized) jedis=pool.getResource();
     jedis.auth(password);
     return jedis;
}
     //toString_________________________________________________________________
@Override
public String toString()
{
     return "["+minidle+","+maxidle+"]"+"\t"+maxtotal+"\t"+maxwait+"\t"+host+"\t"
             +password+"\t"+defaultdb;
}
}
